package com.ruoyi.comparison.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.comparison.domain.TaskResultDetail;
import com.ruoyi.comparison.domain.bo.TaskResultDetailBo;
import com.ruoyi.comparison.domain.vo.TaskResultDetailVo;
import com.ruoyi.comparison.domain.vo.TaskResultVo;
import com.ruoyi.comparison.service.ITaskResultDetailService;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 比对结果详情
 *
 * @author ruoyi
 * @date 2023-11-13
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/resultDetail")
public class TaskResultDetailController extends BaseController {

    private final ITaskResultDetailService iTaskResultDetailService;

    /**
     * 查询比对结果详情列表
     */
    @SaCheckPermission("system:oss:list")
    @GetMapping("/list")
    public TableDataInfo<TaskResultDetailVo> list(TaskResultDetailBo bo, PageQuery pageQuery) {
        return iTaskResultDetailService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询比对结果列表
     */
    @SaCheckPermission("system:oss:list")
    @GetMapping("/formExist")
    public R<TaskResultDetailVo> formExist(Long id) {
        TaskResultDetailVo taskResultDetailVo = iTaskResultDetailService.queryById(id);
        if (ObjectUtil.isEmpty(taskResultDetailVo.getRightFormUrl())||ObjectUtil.isEmpty(taskResultDetailVo.getLeftFormUrl())){
            return R.ok();
        }else {
            return R.ok(taskResultDetailVo);
        }
    }
    /**
     * 导出比对结果详情列表
     */
    @SaCheckPermission("system:oss:list")
    @Log(title = "比对结果详情", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TaskResultDetailBo bo, HttpServletResponse response) {
        List<TaskResultDetailVo> list = iTaskResultDetailService.queryList(bo);
        ExcelUtil.exportExcel(list, "比对结果详情", TaskResultDetailVo.class, response);
    }

    /**
     * 获取比对结果详情详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:oss:list")
    @GetMapping("/{id}")
    public R<TaskResultDetailVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iTaskResultDetailService.queryById(id));
    }

    /**
     * 新增比对结果详情
     */
    @SaCheckPermission("system:oss:list")
    @Log(title = "比对结果详情", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TaskResultDetailBo bo) {
        return toAjax(iTaskResultDetailService.insertByBo(bo));
    }

    /**
     * 修改比对结果详情
     */
    @SaCheckPermission("system:oss:list")
    @Log(title = "比对结果详情", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TaskResultDetailBo bo) {
        return toAjax(iTaskResultDetailService.updateByBo(bo));
    }

}
